﻿using System;
using System.Net.Sockets;
using System.Net;

namespace Smtp_server
{
    public class Relay
    {

        private TcpClient socketForServer;
        private NetworkStream networkStream;
        private System.IO.StreamReader streamReader;
        private System.IO.StreamWriter streamWriter;
        private ILogger ilogger;

        public Relay(ILogger ilogger)
        {
            this.ilogger = ilogger;
        }


        public void Init()
        {
            try
            {
                socketForServer = new TcpClient(Macros.server_relay,Macros.port_relay);
            }
            catch
            {
                return;
            }

            networkStream = socketForServer.GetStream();
            streamReader = new System.IO.StreamReader(networkStream);
            streamWriter = new System.IO.StreamWriter(networkStream);
            ilogger.Write("Relay open connection\r\n");
        }

        public void Close()
        {
            ilogger.Write("Relay close connection\r\n");
            networkStream.Close();
        }


        public bool Send(Email email)
        {
            try
                          
                {
                    ilogger.Write(streamReader.ReadLine());
                    
                    streamWriter.WriteLine("HELO\r\n");
                    streamWriter.Flush();
                    ilogger.Write(streamReader.ReadLine()); 
                    
                    streamWriter.WriteLine("MAIL FROM: <" + email.From + ">\r\n");
                    streamWriter.Flush();
                    ilogger.Write(streamReader.ReadLine()); 
                    
                    streamWriter.WriteLine("RCPT TO: <" + email.To + ">\r\n");
                    streamWriter.Flush();
                    ilogger.Write(streamReader.ReadLine());
                    
                    streamWriter.WriteLine("DATA\r\n");
                    streamWriter.Flush();
                    ilogger.Write(streamReader.ReadLine());
                    
                    streamWriter.WriteLine("from: <" + email.From + ">\r\n" +
                    "to: <" + email.To + ">\r\n" +
                    "date: " + email.Received + "\r\n" +
                    "subject: " + email.Subject + "\r\n" +
                    "\r\n" +
                    email.BodyText + "\r\n" +
                    ".\r\n");
                    streamWriter.Flush();
                    ilogger.Write(streamReader.ReadLine()); 
                    
                    streamWriter.WriteLine("QUIT\r\n");
                    streamWriter.Flush();
                    ilogger.Write(streamReader.ReadLine());

                    ilogger.Write("Relay sent mail\r\n");
                }
            
            catch
            {
                ilogger.Write("Exception reading from Server");
                return false;
            }
            return true;

        }

    }
}


